<?php abstract class mysql implements iDBDriver {
	protected static $Handle     = NULL;
	protected static $ConnStr    = NULL;
	protected static $Table      = NULL;
	protected static $FetchLimit = 1024;
	protected static $Qs = array (
		'Fetch' =>    'select *, unix_timestamp (`time`) as `seconds` from `%table%` where not `seen` order by `id` asc limit %fetch_limit%',
		'See' =>      'update `%table%` set `seen` = TRUE where `id` in (%ids%)',
		'Unsee' =>    'update `%table%` set `seen` = FALSE where `seen`',
		'DropSeen' => 'delete from `%table%` where `seen`',
		'Know' =>     'update `%table%` set `known` = TRUE where `id` in (%ids%)',
		'Drop' =>     'delete from `%table%` where `id` in (%ids%)',
	);
	protected static function Table ($Table) {
		if (NULL === self::$Handle) return (FALSE);
		Out::I (Msg::ConfigApplied, 'table', self::$Table = mysqli_real_escape_string (self::$Handle, $Table));
	}
	protected static function Query (array $Ids = array ()) {
		$Trace = debug_backtrace ();
		if ((count ($Trace) < 2) or (! array_key_exists ('function', $Trace [1])) or (! array_key_exists ($QName = $Trace [1]['function'], self::$Qs))) return (NULL);
		$NewIds = array ();
		foreach ($Ids as $Id) array_push ($NewIds, (int) $Id);
		$Query = str_replace (array ('%table%', '%fetch_limit%', '%ids%'), array (self::$Table, self::$FetchLimit, implode (',', $NewIds)), self::$Qs [$QName]);
		return (mysqli_query (self::$Handle, $Query));
	}
	public static function Open ($Host, $Port, $Database, $User, $Password, $Table, $FetchLimit) {
		if ($FetchLimit >= 1) {
			Out::I (Msg::ConfigApplied, 'fetch_limit', self::$FetchLimit = (int) $FetchLimit);
		} else Out::W (Msg::BadFetchLimit, $FetchLimit, self::$FetchLimit);
		if (($ConnStr = "host=$Host port=$Port dbname=$Database user=$User password=$Password") === self::$ConnStr) {
			Out::I (Msg::NotReconnecting, $ConnStr);
			self::Table ($Table);
			return (TRUE);
		}
		$Handle = @mysqli_connect ($Host, $User, $Password, $Database, $Port);
		if (FALSE === $Handle) {
			Out::W (Msg::ConnectionFail, $ConnStr);
			Out::W (Msg::ConnectionError, 'mysqli', mysqli_connect_errno (), mysqli_connect_error ());
			return (FALSE);
		}
		list (self::$Handle, self::$ConnStr) = array ($Handle, $ConnStr);
		Out::I (Msg::Connected, $ConnStr);
		Out::I (Msg::ConnectionInfo, mysqli_get_host_info ($Handle));
		self::Table ($Table);
		return (TRUE);
	}
	public static function Drop (array $Ids) {
		if (! count ($Ids)) return (0);
		self::Query ($Ids);
		return (mysqli_affected_rows (self::$Handle));
	}
	public static function See (array $Ids) {
		if (! count ($Ids)) return (0);
		self::Query ($Ids);
		return (mysqli_affected_rows (self::$Handle));
	}
	public static function Know (array $Ids) {
		if (! count ($Ids)) return (0);
		self::Query ($Ids);
		return (mysqli_affected_rows (self::$Handle));
	}
	public static function Fetch () {
		$Result = array ();
		$Rows = self::Query ();
		while (NULL !== ($Row = mysqli_fetch_assoc ($Rows))) array_push ($Result, $Row);
		return ($Result);
	}
	public static function Unsee () {
		self::Query ();
		Out::I (Msg::RowsAffected, mysqli_affected_rows (self::$Handle));
	}
	public static function DropSeen () {
		self::Query ();
		Out::I (Msg::RowsAffected, mysqli_affected_rows (self::$Handle));
	}
	public static function __GetRequiredExtensions () {return ('mysqli');}
} ?>